package com.ternnetwork.core.accesscontroll.repository;
import java.util.List;

import org.springframework.data.jpa.repository.Query;

import com.ternnetwork.persistence.repository.BaseRepository;
import com.ternnetwork.core.accesscontroll.entity.Department;



public interface DepartmentRepository extends BaseRepository<Department, Long> {
	/**
	 * 按部门代码称统计
	 * @param code
	 * @return
	 */
	@Query("select count(1) from Department where code=?1")
	Long getCountByCode(String code);
	/**
	 * 排除传入ID，按部门代码称统计	
	 * @param code
	 * @param excludeId
	 * @return
	 */
	@Query("select count(1) from Department where code=?1 and id!=?2")
	Long getCountByCodeAndExcludeId(String code,Long excludeId);
	
	/**
	 * 查找所有
	 */
	@Query("from Department t order by t.seqNum")
	List<Department> findAll();
	
	/**
	 * 按父节点查找所有
	 * @param parentId
	 * @return
	 */
	@Query("from Department t  where t.parentId=?1  order by t.seqNum")
	List<Department> findAllByParentId(Long parentId);
	
	/**
	 * 按父节点统计
	 * @param parentId
	 * @return
	 */
	@Query("select count(1) from Department where parentId=?1")
	Long getCountByParentId(Long parentId);
	
	
	/**
	 * 按ID统计
	 * @param id
	 * @return
	 */
	@Query("select count(1) from Department where id=?1")
	Long getCountById(Long id);


}
